<?php
namespace Admin\Controller;
use Admin\Controller\AdminBaseController;
/**
 * 后台首页控制器
 */
class UserController extends AdminBaseController{

	/**
	 * 用户列表
	 */
	public function index(){
		$count = M('User')->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('User')->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
		$this->display();
	}
	public function add(){
		$this->display();
	}
	public function adddo(){		
			$data['username'] = I('username')?I('username'):null;
			$data['password'] = I('password')?I('password'):null;
			$data['repassword'] = I('repassword')?I('repassword'):null;
			$data['email'] = I('email')?I('email'):null;
			$data['phone'] = I('phone')?I('phone'):null;
			$data['status'] = I('status')?I('status'):null;
			$user=M('user')->find();
			if(empty($data['username'])){
				$this->error('用户名不能为空');
			}
			if($user['username']==$data['username']){
				$this->error('此用户名已经存在');
			}
			if(empty($data['password'])){
				$this->error('请输入密码');
			}
			if(!regex($data['password'],'password')){
				$this->error('密码格式不正确，请输入6-18非空格密码');
			}else{
				$data['password'] = md5(I('password'));
				$data['repassword'] = md5(I('repassword'));
			}
			if($data['password']!=$data['repassword']){
    		$this->error('两次密码不一致');
    	}
			$r = M("User")->add($data);
			if(!$r){
				$this->error('服务器繁忙');
			}
			$this->success('添加成功',U('User/index'));
	}
	public function del(){
        $d=M('user');
        $where['id']=I('id')?I('id'):null;
        $res=$d->where($where)->delete();
        if($res){
            $this->redirect('User/index');
        }else{
            $this->error('删除失败');
        }
    }

    public function edit(){
               
            $where['id']=I(id)?I('id'):null;
            $list=M('user')->where($where)->find();
            //var_dump($list);die();
            $this->assign("list",$list); 
             $this->display();  
    }
    public function editdo(){
    		$where['id']=I(id)?I('id'):null;
    		$list=M('user')->where($where)->find();          
            $data['email']=I('email')?I('email'):null;
            $data['phone']=I('phone')?I('phone'):null;
            $data['status']=I('status')?I('status'):null;
            $data['password']=I('password');
            $user=M('user')->find();
            if($data['password']==''){
				$data['password']=$list['password'];
			}

			if(!regex($data['password'],'password')){
				$this->error('密码格式不正确，请输入6-18非空格密码');
			}else{
				$data['password'] = md5(I('password'));
			}
		//var_dump($data['password']);die;
			 if(!empty($_POST['id'])){
            $list1=M('user')->where($where)->save($data);     
            }
            if($list1){
            	$this->success('修改成功',U('User/index'));
            }else{
            	$this->error('您未修改任何内容');
            }
        
    }

	public function regex($value,$rule) {
    $validate = array(
            'password'  =>  '/\S{6,18}/',
            //'username'  =>  '/\S{6,18}/',
    );
    // 检查是否有内置的正则表达式
    if(isset($validate[strtolower($rule)]))
        $rule       =   $validate[strtolower($rule)];
    return preg_match($rule,$value)===1;
}


}
